package com.lgl.scs.mobile.controller;

import com.lgl.scs.base.controller.BaseController;
import com.lgl.scs.information.service.AddressService;
import com.lgl.scs.mobile.service.MServiceOrderService;
import com.lgl.scs.mobile.service.MUserRateService;
import com.lgl.scs.util.utils.ControllerUtil;
import com.lgl.scsapi.system.model.AppUser;
import com.lgl.scsapi.system.model.PageData;
import com.lgl.scsapi.util.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 移动端前台首页部分
 */

@RestController
@RequestMapping(value="/app/index")
public class AppIndexController extends BaseController {
	private Logger log = LoggerFactory.getLogger(AppIndexController.class);
	@Resource(name="addressService")
	private AddressService addressService;
    @Resource(name="mServiceOrderService")
    private MServiceOrderService mServiceOrderService;
    @Resource(name="mUserRateService")
    private MUserRateService mUserRateService;

	//通过城市名获取城市ID
	@RequestMapping(value="/getCityIdByName",produces="application/json;charset=UTF-8")
	public Object getCityIdByName(HttpServletRequest request) throws Exception {
		String cityName = request.getParameter("cityName");
		if (!StringUtils.hasText(cityName)) {
			return "1";
		}
		String cityId = Constants.hotCityMap.get(cityName);
		if (!StringUtils.hasText(cityId)) {
			cityId = addressService.findCityIdByName(cityName);
		}
		return cityId;
	}
	//通过条件获取区域集合
	@RequestMapping(value="listAreaByCityCode",produces="application/json;charset=UTF-8")
	public Object listAreaByCityCode() {
	    return addressService.listAllArea(getPageData());
	}

	//请求移动端主页的服务订单数据,暂时不考虑从redis中取数据
	@RequestMapping(value="/mIndexPageOrder",produces="application/json;charset=UTF-8")
	public Object mIndexPageOrder() {
		PageData mIndexPd = this.getPageData();
		AppUser loginUser = getSessionAppUser();
		return ControllerUtil.pageOrder(loginUser,mIndexPd,addressService,mServiceOrderService);
	}

    //通过ID查询订单
    @RequestMapping(value="/getOrderById")
    public Object getOrderById(HttpServletRequest request) {
        PageData pd = new PageData(1);
        pd.put("billId", request.getParameter("oid"));
        return mServiceOrderService.getServiceOrderById(pd);
    }
    /**
     * 获取订单相关的评论
     * @return
     */
    @RequestMapping(value = "/listRateByOrder",produces="application/json;charset=UTF-8")
    public Object listRateByOrder() {
        try {
            return mUserRateService.listRateByOrder(getPageData());
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        return null;
    }
}